package hibernate.hibernateInterface;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Expression;

import hibernate.p.DocType;
import hibernate.p.TelNumber;
import hibernate.roomSystem.ReservationForm;
import hibernate.roomSystem.Room;

public class ReservationInterface {

	public static ReservationForm getReservationByID(Long ID,Session session)throws Exception
	{
		Criteria criteria = session.createCriteria(ReservationForm.class);
		Criterion criterion = Expression.eq("id",ID);
		criteria = criteria.add(criterion);
		List resultList = criteria.list();
		if (resultList.size() == 0)
			throw new Exception("ReservationFormNotFoundException");
		else if (resultList.size() > 1)
			throw new Exception("ReserationFormOverlappingException");
		ReservationForm form = (ReservationForm)resultList.get(0);
		return form;
	}
	
	public static List getAllFullTimeReservations(Date timeFrom,Date timeTo,Session session){
		String queryScript="from ReservationForm r where r.partTime= ? and r.timeAccommodation between ? and ?";
		Query query=session.createQuery(queryScript);
		query.setBoolean(0,false);
		query.setDate(1,timeFrom);
		query.setDate(2,timeTo);
		return query.list();
	}
	
	public static List getReservationByTimeCreate(Date timeCreate,Session session)
	{
		Criteria criteria = session.createCriteria(ReservationForm.class);
		Criterion criterion = Expression.eq("timeCreate", timeCreate);
		criteria = criteria.add(criterion);
		List resultList = criteria.list();
		return resultList;
	}
	
	public static List getReservationByTimeAcommodation(Date timeAcom,Session session)
	{
		Criteria criteria = session.createCriteria(ReservationForm.class);
		Criterion criterion = Expression.eq("timeAcommodation", timeAcom);
		criteria = criteria.add(criterion);
		List resultList = criteria.list();
		return resultList;
	}
	
	public static List getAllPartTimeReservations(Date timeFrom,Date timeTo,Session session){
		String queryScript="from ReservationForm r where r.timeAccommodating between ? and ? and r.partTime = ?";
		Query query=session.createQuery(queryScript);
		query.setDate(0,timeFrom);
		query.setDate(1,timeTo);
		query.setBoolean(2,true);
		return query.list();
	}
	
	public static List getReservationByName(String firstName,String middleName,String lastName,String matchCode,Session session)
	{
		String sqlScript = "from ReservationForm r where ";
		String firstNameScript = "";
		String middleNameScript = "";
		String lastNameScript = "";
		if (matchCode.charAt(0) == '1')
		{
			if (Integer.parseInt(matchCode) != 100)
				firstNameScript = "r.firstName = '" + firstName + "' and ";
			else
				firstNameScript = "r.lastName = '" + firstName;
		}
		if (matchCode.charAt(1) == '1')
		{
			if (matchCode.charAt(2) == '1')
				middleNameScript = "r.middleName = '" + middleName + "' and ";
			else
				middleNameScript = "r.middleName = '" + middleName+"'";
		}
		if (matchCode.charAt(2) == '1')
			lastNameScript = "r.lastName = '" + lastName+"'";
		sqlScript += firstNameScript + middleNameScript + lastNameScript;
		System.out.println(sqlScript);
		Query query = session.createQuery(sqlScript);
		List resultList = query.list();
		return resultList;
	}
	
	public static List getReservationByTelNumber(TelNumber telNumber,Session session)
	{
		String queryScript = "from ReservationForm r where r.telNumber1 = '" + telNumber.toString() + "' or r.telNumber2 = '" + telNumber.toString()+"'";
		Query query = session.createQuery(queryScript);
		List resultList = query.list();
		return resultList;
	}
	
	public static List getReservationByNumber(int number,int compareScript,Session session)throws Exception
	{
		String queryScript = "from ReservationForm r where r.number " + CompareCond.parseCompareScript(compareScript) + number;
		Query query = session.createQuery(queryScript);
		List resultList = query.list();
		return resultList;
	}
	
	public static ReservationForm getReservationByDocNumber(String docNumber,Session session)throws Exception
	{
		Criteria criteria = session.createCriteria(ReservationForm.class);
		Criterion criterion = Expression.eq("docNumber",docNumber);
		criteria = criteria.add(criterion);
		List resultList = criteria.list();
		if (resultList.size() == 0)
			throw new Exception("ReservationFormNotFoundException");
		else if (resultList.size() > 1)
			throw new Exception("ReserationFormOverlappingException");
		ReservationForm form = (ReservationForm)resultList.get(0);
		return form;
	}
	
	public static List getReservationByDocType(DocType docType,Session session)
	{
		Criteria criteria = session.createCriteria(ReservationForm.class);
		Criterion criterion = Expression.eq("docType",docType.toString());
		criteria = criteria.add(criterion);
		List resultList = criteria.list();
		return resultList;
	}
	
	public static List getReservationByTimeLeaving(Date timeLeaving,int compareCond,Session session)throws Exception
	{
		String queryScript = "from ReservationForm r where r.timeLeaving =" + CompareCond.parseCompareScript(compareCond).toString() + "?";
		List resultList =session.createQuery(queryScript).setDate(0,timeLeaving).list();
		return resultList;
	}
	
	public static List getAllReservations(Session session){
		String queryScript="from ReservationForm f";
		return session.createQuery(queryScript).list();
	}
}
